const fp = require('lodash/fp')

const cars = [
  { name: 'Ferrari FF', horsePower: 660, dollar_value: 700000, in_stock: true },
  { name: 'Spyker C12 Zagato', horsePower: 650, dollar_value: 648000, in_stock: false },
  { name: 'Jaguar XKR-S', horsePower: 550, dollar_value: 132000, in_stock: false },
  { name: 'Audi R8', horsePower: 525, dollar_value: 114200, in_stock: false },
  { name: 'Aston Martin One-77', horsePower: 750, dollar_value: 1850000, in_stock: true },
  { name: 'Pagani Huayra', horsePower: 700, dollar_value: 1300000, in_stock: false },
]

// let isLastInStock = function(cars) {
//   let last_car = fp.last(cars)
//   return fp.prop('in_stock', last_car)
// }

const isLastInStock = fp.flowRight(fp.curry(fp.prop)('in_stock'), fp.last)

const getFirstCarName = fp.flowRight(fp.curry(fp.prop)('name'), fp.first)

let _average = function(xs) {
  return fp.reduce(fp.add, 0, xs) / xs.length
}

// let averageDollarValue = function(cars) {
//   let dollar_values = fp.map(function(car) {
//     return car.dollar_value
//   }, cars)
//   return _average(dollar_values)
// }

const averageDollarValue = fp.flowRight(_average, fp.curry(fp.map)(car => car.dollar_value))

let _underscore = fp.replace(/\W+/g, '_')

// ['H A'] => ['h_a']
const sanitizeNames = fp.flowRight(
  fp.curry(fp.map)(fp.flowRight(_underscore, fp.toLower)),
  fp.curry(fp.map)(item => item.name)
)

console.log(sanitizeNames(cars))